/*工具库*/

export const test = key => {
  return 'test';
};

//缓存同步设置值
export const setStorageSync = (key,value) => {
  try {
    uni.setStorageSync(key, value);
  } catch (e) {
    console.log(e);
  }
};

//缓存同步获取值
export const getStorageSync = (key,value) => {
  try {
    return uni.getStorageSync(key);
  } catch (e) {
    console.log(e);
  }
};

//同步删除缓存（如果删除全部，注意appInfo）
export const removeStorageSync = (key) => {
  try {
    return uni.removeStorageSync(key);
  } catch (e) {
    console.log(e);
  }
};

//同步删除全部缓存（如果删除全部，注意appInfo）
export const clearStorageSync = () => {
  try {
    uni.clearStorageSync();
  } catch (e) {
    console.log(e);
  }
};

//检测登录权限
export const checkoutAccess = (key) => {
  let bool = false;
  if (getStorageSync('access') && getStorageSync('userInfo') && getStorageSync('token')) {
    bool = true;
  } 
  return bool;
};

//格式化时间
export const formatDateTime = (inputTime,type) => { 
  let time,
      date = new Date(inputTime),
      y = date.getFullYear(),
      m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  let h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  let minute = date.getMinutes();
  let second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  if (type=="Y-M-D") {
    time = y + '-' + m + '-' + d;
  } else {
    time = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  }
  return time;
};

export const formatChineseDateTime = (inputTime,type) => { 
  let time,
      date = new Date(inputTime),
      y = date.getFullYear(),
      m = date.getMonth() + 1,
      d = date.getDate(),
      h = date.getHours(),
      minute = date.getMinutes(),
      second = date.getSeconds();
      
  if (type=="M-D") {
    time = m + '月' + d + '日';
  } else if (type=="Y-M-D") {
    time = y + '年' + m + '月' + d + '日';
  } else {
    time = y + '年' + m + '月' + d + '日' + h + '时' + minute + '分' + second + '秒';
  }
  return time;
}

export const getWeek = (time) => {
  let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
      myDate = new Date(time);  
  return weekDay[myDate.getDay()];
}

//一天最后时间
export const dayDownTime = () => { 
  let dataObj = new Date();
  let todayYear= dataObj.getFullYear();
  let todayMonth= dataObj.getMonth();
  let todayDay= dataObj.getDate();
  let todayTime=(new Date(todayYear,todayMonth,todayDay,'23','59','59')).getTime();
  return formatDateTime(todayTime);
}

export const systemInfo = () => {
  try {
      const res = uni.getSystemInfoSync();
      return res;
      /*
      console.log(res.model);
      console.log(res.pixelRatio);
      console.log(res.windowWidth);
      console.log(res.windowHeight);
      console.log(res.language);
      console.log(res.version);
      console.log(res.platform);
      */
  } catch (e) {
    console.log(e);
  }
}

export const diffVersion = (curV,reqV) => {            
  let arr1 = curV.split('.');
  let arr2 = reqV.split('.');
  let minL = Math.min(arr1.length,arr2.length);  
  let pos = 0;        //当前比较位
  let diff = 0;        //当前为位比较是否相等
  let bool = false;    //默认不需要更新
  while(pos<minL){
      diff = parseInt(arr1[pos])-parseInt(arr2[pos]);  
      if(diff!=0){  
        break;  
      } 
      pos++;                  
  }
  if (diff>0) {
    //console.log('新版本');
    bool = true;
  }else if (diff==0) {
    //console.log('稳定版');
  }else{
    //console.log('旧版本');
  }
  return bool;
}


